home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 001 / mnemosyn / StubsHack / Sources / c / StubsHack
Text File  |  1994-09-02  |  3KB  |  92 lines

  1. /* ⌐ Julian Smith 1994    */
  2.  
  3. #include "kernel.h"
  4.  
  5. #include "StubsHack.StubsHack.h"
  6.  
  7.  
  8.  
  9. StubsHack_mallocfn    StubsHack_scl_malloc    = malloc;
  10. StubsHack_freefn    StubsHack_scl_free    = free;
  11. StubsHack_reallocfn    StubsHack_scl_realloc    = realloc;
  12. StubsHack_callocfn    StubsHack_scl_calloc    = calloc;
  13.  
  14.  
  15.  
  16. StubsHack_fnptr    StubsHack_GetDestOfB( StubsHack_fnptr address)
  17.     /* Doesn't work too well for negative ofsets, but all offsets to    */
  18.     /* the shared c lib are positive.                    */
  19. {
  20. unsigned int    int_address    = (unsigned int) address;
  21. unsigned int    instruction    = *((unsigned int *) ((int) address));
  22.  
  23. if ( (instruction & 0xFF000000) != 0xEA000000)    return NULL;
  24.  
  25. instruction &= 0x00FFFFFF;    /* Get the 24-bit offset.    */
  26.  
  27. return (StubsHack_fnptr) ( int_address + 8/*pipeline*/ + 4*instruction);
  28.     /* This line returns a warning:
  29.         <cast>: cast between function pointer and non-function object
  30.     */
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. StubsHack_error    StubsHack_ReplaceANSIAllocFns(
  38.         StubsHack_mallocfn    new_malloc,
  39.         StubsHack_reallocfn    new_realloc,
  40.         StubsHack_callocfn    new_calloc,
  41.         StubsHack_freefn    new_free,
  42.  
  43.         StubsHack_mallocfn    *old_malloc_store,
  44.         StubsHack_reallocfn    *old_realloc_store,
  45.         StubsHack_callocfn    *old_calloc_store,
  46.         StubsHack_freefn    *old_free_store,
  47.  
  48.         BOOL            make_stack_allocater_be_mallocfree
  49.         )
  50. {
  51. static BOOL        firsttime    = TRUE;
  52. StubsHack_mallocfn    old_malloc;
  53. StubsHack_reallocfn    old_realloc;
  54. StubsHack_callocfn    old_calloc;
  55. StubsHack_freefn    old_free;
  56.  
  57. old_malloc    = (StubsHack_mallocfn)    StubsHack_GetDestOfB( (StubsHack_fnptr) malloc);
  58. old_realloc    = (StubsHack_reallocfn)    StubsHack_GetDestOfB( (StubsHack_fnptr) realloc);
  59. old_calloc    = (StubsHack_callocfn)    StubsHack_GetDestOfB( (StubsHack_fnptr) calloc);
  60. old_free    = (StubsHack_freefn)    StubsHack_GetDestOfB( (StubsHack_fnptr) free);
  61.  
  62. if ( !old_malloc || !old_realloc || !old_calloc || !old_free)    return StubsHack_error_NOTSTUBS;
  63.  
  64. if (firsttime)    {
  65.     StubsHack_scl_malloc    = old_malloc;
  66.     StubsHack_scl_realloc    = old_realloc;
  67.     StubsHack_scl_calloc    = old_calloc;
  68.     StubsHack_scl_free    = old_free;
  69.     }
  70.  
  71. if ( make_stack_allocater_be_mallocfree)
  72.     _kernel_register_allocs( StubsHack_scl_malloc, StubsHack_scl_free);
  73.  
  74. if (old_malloc_store)    *old_malloc_store    = old_malloc;
  75. if (old_realloc_store)    *old_realloc_store    = old_realloc;
  76. if (old_calloc_store)    *old_calloc_store    = old_calloc;
  77. if (old_free_store)    *old_free_store        = old_free;
  78.  
  79. if (new_malloc)        StubsHack_MakeBranchInstruction( malloc,    new_malloc);
  80. if (new_realloc)    StubsHack_MakeBranchInstruction( realloc,    new_realloc);
  81. if (new_calloc)        StubsHack_MakeBranchInstruction( calloc,    new_calloc);
  82. if (new_free)        StubsHack_MakeBranchInstruction( free,        new_free);
  83.  
  84. firsttime = FALSE;
  85. return StubsHack_error_OK;
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.